package com.dy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dy.mapper.DyTypeMapper;
import com.dy.pojo.dto.DyTypeDTO;
import com.dy.pojo.entity.DyTypeEntity;
import com.dy.pojo.entity.DyUserEntity;
import com.dy.service.DyTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName:
 * @Author: 仄言in
 * @Desctiption: (描述)
 * @Date: 2021/6/9 16:17
 * @Version 1.0 (版本)
 */
@Service
public class DyTypeServiceImpl extends ServiceImpl<DyTypeMapper, DyTypeEntity> implements DyTypeService {
    @Autowired
    private DyTypeMapper dyTypeMapper;

    @Override
    public Page<DyTypeEntity> selectPage(Integer page, Integer pageSize) {
        Page page1 = new Page(page, pageSize);
        Page selectPage = dyTypeMapper.selectPage(page1, null);
        return selectPage;
    }

    @Override
    public boolean create(String name) {
        DyTypeEntity selectOne = dyTypeMapper.selectOne(new QueryWrapper<DyTypeEntity>().eq("name", name));
        if (selectOne != null) {
            return false;
        }
        DyTypeEntity entity = new DyTypeEntity();
        String code="T"+System.currentTimeMillis();
        entity.setCode(code).setName(name);
        dyTypeMapper.insert(entity);
        return true;
    }

    @Override
    public void deleteType(String code) {
        dyTypeMapper.delete(new QueryWrapper<DyTypeEntity>().eq("code",code));
    }

    @Override
    public void updateType(String code, String name) {
        DyTypeEntity dyTypeEntity = dyTypeMapper.selectOne(new QueryWrapper<DyTypeEntity>().eq("code", code));
        dyTypeEntity.setCode(code).setName(name);
        dyTypeMapper.update(dyTypeEntity,new QueryWrapper<DyTypeEntity>().eq("code",dyTypeEntity.getCode()));
    }

    @Override
    public DyTypeEntity queryTypeByCode(String code) {
        DyTypeEntity dyTypeEntity = dyTypeMapper.selectOne(new QueryWrapper<DyTypeEntity>().eq("code", code));
        return dyTypeEntity;
    }

}
